#! /usr/bin/env python
#**coding=utf-8
from util import closure,format,compileIt,document,link,createPathIfNotExist
import os
#comment

class Builder(object):
	"""docstring for Builder"""
	def __init__(self,mainFile,srcPath="src"):
		super(Builder, self).__init__()
		self.mainFileName = mainFile
		self.srcPath = os.path.basename(self.mainFileName)
		self.closureLibraryPath = "..\\..\\closure-library"
		self.jsDocPath = "tools\\jsdoc"
		self.binPath = "bin"
		self.srcPath=srcPath
	
	def createSrc(self):
		srcPath = self.binPath + "\\src"
		createPathIfNotExist(srcPath)
		srcDocPath = srcPath + "\\docs"
		for item in self._getSrcFiles(self.srcPath):
			filePath = srcPath + "\\" + item['path']
			print filePath
			createPathIfNotExist(filePath)
			fileName = item['path'] + "\\" + item['fileName']
			createPathIfNotExist(filePath)
			formatName = format(fileName, output=filePath)
		document(srcPath+"\\src",jsdoc=self.jsDocPath,output=srcDocPath)
		print "CREATE SRC COMPLETE...\n"
	
	def _getSrcFiles(self,srcPath):
		l = []
		for root,dirs,files in os.walk(srcPath):
			for fileName in files:
				if fileName.endswith(".js"):
					pp = root
					if not len(pp):
						pp="."
					l.append({"path":pp,"fileName":fileName})
		return l
	
	def link(self):
		linkedPath = self.binPath + "\\linked"
		createPathIfNotExist(linkedPath)
		outputLinkedPath = linkedPath + "\\src"
		createPathIfNotExist(outputLinkedPath)
		
		linkedFileName = link(self.mainFileName,lib=self.closureLibraryPath,output=outputLinkedPath)
		linkedFormatedName = format(linkedFileName,outputLinkedPath)
		
		outputDocs = linkedPath + "\\docs"
		document(linkedFormatedName,jsdoc=self.jsDocPath,output=outputDocs)
		print "LINK COMPLETE...\n"
	
	def compile(self):
		compiledPath = self.binPath + "\\compiled"
		createPathIfNotExist(compiledPath)
		
		compiledFileName = compileIt(self.mainFileName,lib=self.closureLibraryPath,output=compiledPath)
		format(compiledFileName,output=compiledPath)
		closure(compiledFileName,output=compiledPath)
		print "COMPILE COMPLETE...\n"
	

"""
#! /usr/bin/env python
#coding=utf-8

from distutils.core import setup


setup(
    name='MyLib',
    version='1.0',
    description='My Lib disribution Utility',
    author='Edison',
    author_email='eddy.wd5@gmail.com',
    url='http://hi.baidu.com/gylxue',
    py_modules=['mylib'],
)
"""
